package com.example.oasystem.controller.position;

import com.example.oasystem.entity.dept.AoaDept;
import com.example.oasystem.entity.notepaper.AoaNotepaper;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.position.AoaPosition;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AoaPositionController {
    String sousuo=null;
    @Autowired
    AoaPositionService aoaPositionService;
//    @RequestMapping("positionmanage")
//    public String positionmanage(Model model){
//        List<AoaPosition> aoaPositions = aoaPositionService.aoaPosition();
//        model.addAttribute("positions",aoaPositions);
//        return "user/positionmanage";
//    }
    @RequestMapping("positionedit")
    public String positionedit(HttpServletRequest request,Model model){
        String positionid = request.getParameter("positionid");


        List<AoaDept> depts = aoaPositionService.aoaDepts();
        for (AoaDept dept : depts) {
            System.out.println("dept = " + dept);
        }
        model.addAttribute("depts",depts);
        if (positionid!=null&&positionid!=""){
            long positionId = Long.parseLong(positionid);

            AoaPosition aoaPosition = aoaPositionService.selectByPrimaryKey(positionId);

            model.addAttribute("position",aoaPosition);

            AoaDept aoaDept = aoaPositionService.aoaDept(aoaPosition.getDeptid());

            model.addAttribute("positiondept",aoaDept);

            return "/user/positionedit";
        }
        else {
            return "/user/positionedit";
        }
    }
    @RequestMapping("positionUpdate")
    public String positionUpdate(AoaPosition aoaPosition){
        if (aoaPosition.getPositionId()!=null){
            aoaPositionService.updateByPrimaryKeySelective(aoaPosition);
        }
        else {
            aoaPositionService.insertSelective(aoaPosition);
        }
        return "forward:/positionmanage";
    }


    @RequestMapping("deletePositon")
    public String deletePositon(HttpServletRequest request, Model model,@RequestParam("page") int page) throws IOException {
        long positionId = Long.parseLong(request.getParameter("positionId"));
        System.out.println("positionId = " + positionId);
        List<AoaUser> users = aoaPositionService.users(positionId);
        for (AoaUser user : users) {
            System.out.println("user = " + user);
        }
        if (null == users || users.size() ==0)
        {
            int i = aoaPositionService.deleteByPrimaryKey(positionId);

        }
        else{
            model.addAttribute("errormess","此职级还有员工，不能删除");
        }
        return "forward:positionmanage?page="+page;
    }

    @RequestMapping("/positionmanage")
    public String positionmanage(@RequestParam(value = "search", required = false,defaultValue = "")String search,@RequestParam(value = "page", required = false,defaultValue = "0") int number,Model model){
        System.out.println(111111);
        System.out.println("search = " + search);
        if (search.equals(""))
        {
            search=null;
            sousuo=null;

        }
        else
        {
            sousuo=search;
        }
        List<AoaPosition> aoaPositions = aoaPositionService.aoaPosition(search);
        int totalElement= aoaPositions.size();
        int size=4;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;
        boolean first;
        boolean last;
        if (number==0)
        {
            first=true;
        }
        else
        {
            first=false;
        }

        if (number==(totalPages-1))
        {
            last=true;
        }
        else
        {
            last=false;
        }
        Page fenye=new Page(totalElement,size,totalPages,number, first,last);
        List<AoaPosition> positions = aoaPositionService.paginationPosition(search, (number) * size, size);
        if(search==null)
        {
            search="";

        }
        model.addAttribute("search",search);
        model.addAttribute("positions",positions);
        model.addAttribute("page",fenye);
        model.addAttribute("url","position");
        return "/user/positionmanage";
    }
    @RequestMapping("/position")
    public String position(int page,Model model){
        List<AoaPosition> aoaPositions = aoaPositionService.aoaPosition(sousuo);
        int totalElement= aoaPositions.size();
        int size=4;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;
        int number=page;
        boolean first;
        boolean last;
        if (number==0)
        {
            first=true;
        }
        else
        {
            first=false;
        }

        if (number==(totalPages-1))
        {
            last=true;
        }
        else
        {
            last=false;
        }
        Page fenye=new Page(totalElement,size,totalPages,number, first,last);
        List<AoaPosition> positions = aoaPositionService.paginationPosition(sousuo, (number) * size, size);
        model.addAttribute("positions",positions);
        model.addAttribute("search",sousuo);
        model.addAttribute("page",fenye);
        model.addAttribute("url","position");
        return "/user/position";
    }
}
